#!/usr/bin/perl
#nasmirror-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

#get rsync jobs list from crontab
sub get_crontab_rsync{
    open(CRON,"/sysvol/etc/cronfile");
    my @cron=<CRON>;
    close(CRON);
    return @cron;
}

#list rsync jobs in index.cgi
sub list_jobs{

    my @cron=&get_crontab_rsync();
    my $lines=@cron;
    $lines=$lines-2;
        print "<table width=100% class=mode2><tr>\n";
        print "<td  valign=top><div class=main><table  class=mode1 border=0 bordercolor=#33ccff cellspacing=0 cellpadding=1 width=100%>\n";
        print "<tr class=title1> <th align=left>$text{'mirror_ip'}</th> ",
                "<th align=left>$text{'mirror_modname'}</th> ",
                "<th align=left>$text{'mirror_path'}</th>",
                "<th align=left>$text{'mirror_type'}</th> ",
                "<th align=left>$text{'mirror_rule'}</th>",
                "<th align=left>$text{'bindwidth'}</th> </tr>\n";
        my $i=0;
        foreach (@cron) {
             
                $cron_line=$_;
                @line_info=&get_crontab_info($cron_line);
                $line_min=$line_info[0];
                $line_hour=$line_info[1];
                $line_day=$line_info[2];
                $line_month=$line_info[3];
                $line_week=$line_info[4];
                
                $line_ipmod=$line_info[6];
                $line_path=$line_info[7];
#               $line_opt=$line_info[8].$line_info[9];
                $line_opt=&get_opt_text($line_info[8], $line_info[9]);
                @ip_mod=split(/::/, $line_ipmod);
                $line_ip=$ip_mod[0];
                $line_mod=$ip_mod[1];
                $line_time=&get_rule_text($line_month, $line_day, $line_hour, $line_min); 
                $line_bw="Unlimit";
		$line_bw=$text{'unlimit'};
                foreach(@line_info){
                   if($_=~ /wlimit=(\d+)/){
                      @line_bwidth=split(/=/, $_);
#                      $line_bw=@line_bwidth[1];
		       $spd = $1 / 1024;
                       $line_bw=$spd."MB/s";
                      last;
                   }
                }
                if ($i % 2 eq 0)
                {
                        $dismode = "class=mode1";

                }
                else
                {
                        $dismode = "class=mode1";

                }
		if($line_ip){
                print "<tr $dismode>\n";
                print "<td><input type=radio name=sharedir value=\"$cron_line\">$line_ip</td>\n";

                printf "<td nowarp $dismode>$line_mod</td>\n";
                printf "<td nowarp $dismode>$line_path</td>\n";
                my $stype;

                print "<td nowarp width=15%>$line_opt</td>\n";
                print "<td nowarp $dismode>$line_time</td>\n";
                printf "<td nowarp $dismode>$line_bw</td> </tr>\n";
		}
                $i++;
        }
        print "</table></div></td>\n";

        print "<td valign=top width=160><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
        print "<tr><td align=center><input type=submit name=exec value=\"$text{'mirror_exec'}\" onclick='return on_exec_click($lines);' class=button></td></tr>\n";
	
        printf "<tr><td align=center><input type=submit name=config value=\"$text{'mirror_conf'}\" onclick='return on_config_click($lines);' class=button></td></tr>\n";
        printf "<tr><td align=center><input type=submit name=jobdel value=\"$text{'mirror_delete_job'}\" onclick='return on_jobdel_click($lines);' class=button></td></tr>\n";
        print "<tr><td align=center><input type=submit name=newmirror value=\"$text{'mirror_create_mirror'}\" onclick=\'return on_newmirror_click();\' class=button></td></tr>\n";
#        printf "<tr><td align=center><input type=submit name=newjob value=\"$text{'mirror_create_job'}\" onclick='return on_newjob_click();' class=button></td></tr>\n";
        
        print "</table></td></tr>";
        #print "<tr><td colspan=2><i>hint</i></td></tr>";
        print "</table>\n";






}

#config a rsync job
sub get_rule_text{
    my $minute=$text{'mirror_minute'};
    my $hour=$text{'mirror_hour2'};
    my $day=$text{'mirror_day2'};
    my $month=$text{'mirror_month2'};
    if($_[0] eq "*"){
       $_[0]=$text{'mirror_every'};
    }
    if($_[1] eq "*"){
       $_[1]=$text{'mirror_every'};
    }
    if($_[2] eq "*"){
       $_[2]=$text{'mirror_every'};
    }

    my $ruleinfo=$_[0].$month." ".$_[1].$day." ".$_[2].$hour." ".$_[3].$minute;
    return $ruleinfo;
}

sub get_opt_text{
    my $optu=$text{'mirror_optu'};
    my $optd=$text{'mirror_optd'};
    my $optline="";
    if($_[0] =~ /u/){
        $optline=$optline.$optu;
    }
    if($_[1] =~ /elet/){
        $optline=$optline." ".$optd;
    }
    if($optline eq ""){
        $optline=$text{'mirror_opta'};
    }
    return $optline;
}

#run a rsync operation
sub exec_rsync{

}
#get infomation from a crontab line
sub get_crontab_info{
    my @ss=split(/ /, $_[0]);
    return @ss;
  
}

sub is_exit_usrvol{
   my $volmark=0;
   open(FH, "/etc/nas/voltab");
   my @vol=<FH>;
   close(FH);
   $line_num=@vol;
   if($line_num>1){
      if($vol[1] =~ /^volume/){

          $volmark=1;
   
      }
   }
   return $volmark;
}
1;
